package display 
{
	import controller.Controller;
	import events.DisplayEvent;
	import flash.display.MovieClip;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import utils.DrawingShapes;
	
	/**
	 * ...
	 * @author Henrik Riise Hansen
	 */
	
	public class DisplaySearchField extends MovieClip 
	{
		// controller
		
		private var control:Controller;
		
		// sprite holder
		
		private var holder:Sprite;
		
		// colors
		
		private var searchResultBackgroundColor:uint;
		private var searchRowBackgroundColor:uint;
		private var searchResultAlternateRowBackgroundColor:uint;
		private var searchFieldFontColor:uint;
		private var searchFieldGradientColorOne:uint;
		private var searchFieldGradientColorTwo:uint;
		
		private var buttonColorOne:uint;
		private var buttonColorTwo:uint;
		private var buttonFontColor:uint;
		
		// highlight
		
		private var mainColorhighlight:DisplayHighLight;
		
		public function DisplaySearchField(ctr:Controller) 
		{
			control = ctr;
			
			control.addEventListener(DisplayEvent.COLOR_CHANGED, onColorChanged);
			control.addEventListener(DisplayEvent.VISIBILITY_CHANGED, onVisibilityChanged);
			
			build();
		}
		
		// events
		
		private function onColorChanged(e:DisplayEvent):void
		{
			switch(e.data.itemCategory)
			{
				case 'buttonFontColor':
				buttonFontColor = e.data.colorOne;
				break;
				
				case 'buttonGradient':
				buttonColorOne = e.data.colorOne;
				buttonColorTwo = e.data.colorTwo;
				break;
				
				case 'searchFieldGradient':
				searchFieldGradientColorOne = e.data.colorOne;
				searchFieldGradientColorTwo = e.data.colorTwo;
				break;
				
				case 'searchFieldFontColor':
				searchFieldFontColor = e.data.colorOne;
				break;
				
				case 'searchResultBackgroundColor':
				searchResultBackgroundColor = e.data.colorOne;
				break;
				
				case 'searchRowBackgroundColor':
				searchRowBackgroundColor = e.data.colorOne;
				break;
			}
			
			createSearchField();
		}
		
		private function onVisibilityChanged(e:DisplayEvent):void
		{
			if (e.data.item == 'SearchField')
			{
				this.visible = e.data.selected;
			}
		}
		
		private function createSearchIcon():Sprite
		{
			var searchIcon:Sprite = new Sprite;
			var line:Sprite = new Sprite;
			
			searchIcon.graphics.lineStyle(2, searchFieldFontColor);
			searchIcon.graphics.drawCircle(8, 8, 5);
			searchIcon.graphics.endFill();
			
			line.graphics.lineStyle(2, searchFieldFontColor);
			line.graphics.moveTo(12, 12);
			line.graphics.lineTo(16, 16);
			line.graphics.endFill();
			
			searchIcon.addChild(line);
			
			return searchIcon;
		}
		
		private function createSearchField():void
		{
			var searchBackground:Sprite;
			var searchButton:Sprite;
			var searchIcon:Sprite;
			var buttonFontHigh:DisplayHighLight;
			var buttonFontColor:DisplayHighLight;
			
			mainColorhighlight = new DisplayHighLight(control, 'mainColor', 20, 260);
			buttonFontHigh = new DisplayHighLight(control, 'buttonFontColor', 20, 260);
			buttonFontColor = new DisplayHighLight(control, 'buttonGradient', 20, 260);
			
			searchBackground = createButton(searchFieldGradientColorOne, searchFieldGradientColorTwo, 20, 260, 0, 0, 0, 0, true);
			searchButton = createButton(buttonColorOne, buttonColorTwo, 20, 20, 0, 0, 0, 0, true);
			searchIcon = createSearchIcon();
			searchButton.x = 240;
			
			searchButton.addChild(searchIcon);
			searchBackground.addChild(searchButton);
			searchBackground.addChild(mainColorhighlight);
			searchBackground.addChild(buttonFontHigh);
			searchBackground.addChild(buttonFontColor);
			
			holder.addChild(searchBackground);
		}
		
		private function createSearchFieldResultPane():void
		{
			
		}
		
		private function createButton(_colorOne:uint,_colorTwo:uint,_height:int,_width:int,_topLeft:int,_topRight:int,_bottomLeft:int,_bottomRight:int,_haveLine:Boolean):Sprite
		{
			var matr:Matrix = new Matrix();
			var sprMethod:String = SpreadMethod.PAD;
			var sButton:Sprite = new Sprite;
			
			matr.createGradientBox(_width,_height, Math.PI/2, 0, 0);
			
			if (_haveLine)
			{
				sButton.graphics.lineStyle(1, searchFieldGradientColorOne);
			}
			
			sButton.graphics.beginGradientFill('linear', [_colorOne, _colorTwo], [1, 1], [0, 255], matr, sprMethod);
			sButton.graphics.drawRoundRectComplex(0, 0, _width, _height, _topLeft, _topRight, _bottomLeft, _bottomRight);
			
			return sButton;
		}
		
		private function clear():void
		{
			while (holder.numChildren != 0)
			{
				holder.removeChildAt(0);
			}
		}
		
		private function build():void
		{
			holder = new Sprite;
			holder.x = 490;
			holder.y = 64;
			
			addChild(holder);
		}
	}
}